package com.beerq.view_manager;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.beerq.BuildConfig;
import com.beerq.R;
import com.beerq.util.BasicHttp;
import com.beerq.util.LogX;
import com.beerq.util.StringUtil;
import com.beerq.view.BaseActivity;

/**
 * 作者：minj on 2017/9/27 09:39
 * 获取验证码管理类
 */
public class ValidateManager {

    private  static final int REREQUEST_AUTH_CODE_DURATION = BuildConfig.DEBUG?10:60;
    private  static final int MSG_START_TIMEOUT = 1;
    private   int mSecond;

    private  Button btn_getValiCode;

    private  TextView txtPhoneNo;

    private  String latestValidateCode;

    private long currentTime;

    private BaseActivity bindActivity;

    public ValidateManager(BaseActivity activity){
        bindActivity=activity;
    }

    public  boolean checkValidateCode(String code){
        if(StringUtil.isEmpty(code)){
            return false;
        }

        if(StringUtil.isEmpty(latestValidateCode)){
            bindActivity.showToast("请获取验证码");
            return false;
        }

        if(System.currentTimeMillis()-currentTime>1000*60*5){
            bindActivity.showToast("验证码已过期，请重新获取");
            return false;
        }

        if(!code.equals(latestValidateCode)){
            bindActivity.showToast("验证码不正确，请重新输入");
            return false;
        }

        return true;
    }

    public  void bind(){

        btn_getValiCode=(Button) bindActivity.findViewById(R.id.btn_getValiCode);
        txtPhoneNo = (TextView) bindActivity.findViewById(R.id.edit_phone_number);
        btn_getValiCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNo = txtPhoneNo.getText().toString();
                if (StringUtil.isEmpty(phoneNo)) {
                    bindActivity.showToast("手机号码不能为空");
                    return;
                } else if (phoneNo.length() != 11) {
                    bindActivity.showToast("手机号码格式不正确");
                    return;
                }

                btn_getValiCode.setEnabled(false);
                mSecond = REREQUEST_AUTH_CODE_DURATION;

                if(!RegisterAndLoginManager.getValidateCode(bindActivity, phoneNo, mIMyCallBack)){
                    btn_getValiCode.setEnabled(true);
                }
            }
        });
    }

    BasicHttp.IMyCallBack mIMyCallBack=new BasicHttp.IMyCallBack() {
        @Override
        public void onResponse(String resonseData) {
            JSONObject jsonObject= JSON.parseObject(resonseData);
            latestValidateCode=jsonObject.getString("Result");

            LogX.d("ccc",latestValidateCode);

            currentTime=System.currentTimeMillis();

            Message message = handler.obtainMessage(MSG_START_TIMEOUT);
            handler.sendMessageDelayed(message, 1000);
        }

        @Override
        public void onFailure(String resonseData) {

        }
    };


     Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_START_TIMEOUT:
                    if (mSecond == REREQUEST_AUTH_CODE_DURATION) {
                        btn_getValiCode.setEnabled(false);
                    }
                    mSecond--;
                    btn_getValiCode.setText(mSecond + "s");

                    if (mSecond > 0) {
                        Message message = handler.obtainMessage(MSG_START_TIMEOUT);
                        handler.sendMessageDelayed(message, 1000);
                    } else {
                        btn_getValiCode.setText("获取验证码");
                        btn_getValiCode.setEnabled(true);
                    }
            }

            super.handleMessage(msg);
        }
    };

}
